package com.hl.dao;

import com.hl.pojo.PanghuShop;
import com.hl.pojo.PanghuShopGoods;
import com.hl.util.JdbcUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class ShopDaoimpl implements ShopDao{
    QueryRunner queryRunner = new QueryRunner(JdbcUtil.dataSource);
    @Override
    public List<PanghuShop> search(String pageNum, String pageSize, String phone, String keyword, String shopStatus, String startTime, String endTime) {
        String sql = null;
        List<PanghuShop> list = null;
        if (phone!=""&&keyword!=""&&startTime!=""&&endTime!=""){
            sql = "select shop_id as shopId,shop_name as shopName," +
                    " shop_describe as shopDescribe,shop_image as shopImage," +
                    "shop_phone as shopPhone," +
                    " shop_province as shopProvince,shop_city as shopCity," +
                    " shop_district as shopDistrict,shop_address as shopAddress, +" +
                    " shop_longitude as shopLongitude,shop_latitude as shopLatitude," +
                    " shop_status as shopStatus,add_time as addTime," +
                    " shop_user_id as shopUserId,shop_account as shopAccount," +
                    " shop_password as shopPassword,shop_sort as shopSort " +
                    " from panghu_shop where shop_phone like ? and shop_name like ?" +
                    "and add_time between ? and ?";
        }else {
            sql = "select shop_id as shopId,shop_name as shopName," +
                    "shop_describe as shopDescribe,shop_image as shopImage," +
                    "shop_phone as shopPhone," +
                    "shop_province as shopProvince,shop_city as shopCity," +
                    "shop_district as shopDistrict,shop_address as shopAddress," +
                    "shop_longitude as shopLongitude,shop_latitude as shopLatitude," +
                    "shop_status as shopStatus,add_time as addTime," +
                    "shop_user_id as shopUserId,shop_account as shopAccount," +
                    "shop_password as shopPassword,shop_sort as shopSort " +
                    "from panghu_shop";
        }
        try {
            if (phone!=""&&keyword!=""&&startTime!=""&&endTime!=""){
                //搜索
                list = queryRunner.query(sql,new BeanListHandler<>(PanghuShop.class), "%"+phone+"%","%"+keyword+"%",startTime,endTime);
            }else {
                //展示
                list = queryRunner.query(sql,new BeanListHandler<>(PanghuShop.class));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int save(PanghuShop shop) {
        int num = 0;
        String sql = "insert into panghu_shop " +
                "(shop_name,"+
                "shop_describe,shop_image,"+
                "shop_phone," +"shop_province," +
                "shop_city," + "shop_district," +
                "shop_address," + "shop_longitude," +
                "shop_latitude," + "shop_status," +
                "add_time," + "shop_user_id," +
                "shop_account," + "shop_password," +
                "shop_sort) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

        try {
            num = queryRunner.update(sql,shop.getShopName(),shop.getShopDescribe(),shop.getShopImage(),shop.getShopPhone(),shop.getShopProvince(),shop.getShopCity(),shop.getShopDistrict(),shop.getShopAddress(),shop.getShopLongitude(),shop.getShopLatitude(),shop.getShopStatus(),shop.getAddTime(),shop.getShopUserId(),shop.getShopAccount(),shop.getShopPassword(),shop.getShopSort());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num;
    }

    @Override
    public int update(PanghuShop shop) {
        int num = 0;
//        "insert into panghu_shop " +
//                "(shop_name,"+
//                "shop_describe,shop_image,"+
//                "shop_phone," +"shop_province," +
//                "shop_city," + "shop_district," +
//                "shop_address," + "shop_longitude," +
//                "shop_latitude," + "shop_status," +
//                "add_time," + "shop_user_id," +
//                "shop_account," + "shop_password," +
//                "shop_sort) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        String sql = "update panghu_shop set shop_name = ?," +
                "shop_describe = ?, shop_image = ?," +
                "shop_phone = ?, shop_province = ?," +
                "shop_city = ?,shop_district = ?," +
                "shop_address = ?,shop_longitude = ?," +
                "shop_latitude = ?,shop_status = ?," +
                "shop_user_id = ?," +
                "shop_account = ?, shop_password = ?," +
                "shop_sort = ? where shop_id = ?";
        try {
            num = queryRunner.update(sql,shop.getShopName(),shop.getShopDescribe(),shop.getShopImage(),shop.getShopPhone(),shop.getShopProvince(),shop.getShopCity(),shop.getShopDistrict(),shop.getShopAddress(),shop.getShopLongitude(),shop.getShopLatitude(),shop.getShopStatus(),shop.getShopUserId(),shop.getShopAccount(),shop.getShopPassword(),shop.getShopSort(),shop.getShopId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num;
    }

    @Override
    public List<PanghuShopGoods> displayShopManage(String pageNun, String pageSize, String keyword, String goodsStatus, String shopId, String startTime, String endTime) {
        List<PanghuShopGoods> list = null;
        String sql = "select goods_id as goodId," + "goods_name as goodName," +
                "goods_image as goodsImage," + "goods_present_price as goodsPresentPrice," +
                "shop_id as shopId," + "shop_category_id as shopCategoryId," +
                 "goods_unit as goodUnit," +
                "goods_sort as goodsSort," + "goods_status as goodsStatus FROM panghu_shop_goods where shop_id = ?";
        try {
            list = queryRunner.query(sql,new BeanListHandler<>(PanghuShopGoods.class),shopId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int increaseGoods(PanghuShopGoods panghuShopGoods) {
        int num = 0;
        String sql = "insert into panghu_shop_goods(goods_name,goods_image,goods_present_price,shop_id,shop_category_id,goods_unit,goods_status) values(?,?,?,?,?,?,?)";
        try {
            num = queryRunner.update(sql,
         panghuShopGoods.getGoodsName(),panghuShopGoods.getGoodsImage(),panghuShopGoods.getGoodsPresentPrice(),panghuShopGoods.getShopId(),panghuShopGoods.getShopCategoryId(),panghuShopGoods.getGoodsUnit(),panghuShopGoods.getGoodsStatus());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num;
    }

    @Override
    public int updateShopGoods(PanghuShopGoods panghuShopGoods) {
        int num = 0;
        String sql = "update panghu_shop_goods set goods_name = ?,goods_image = ?,goods_present_price = ? where goods_id = ?";
        try {
            num = queryRunner.update(sql,panghuShopGoods.getGoodsName(),panghuShopGoods.getGoodsImage(),panghuShopGoods.getGoodsPresentPrice(),panghuShopGoods.getShopId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num;
    }
}
